package mine.network;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.whu.administrator.ws.MyApplication;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * 下班签到
 * Created by Administrator on 2016/2/29.
 */
public class UserLeave {
    Delegate delegate;
    Leave leave = new Leave();
    ResponseLeave responseLeave = new ResponseLeave();

    public Delegate getDelegate() {
        return delegate;
    }

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public Leave getLeave() {
        return leave;
    }

    public void setLeave(Leave leave) {
        this.leave = leave;
    }

    public ResponseLeave getResponseLeave() {
        return responseLeave;
    }

    public void setResponseLeave(ResponseLeave responseLeave) {
        this.responseLeave = responseLeave;
    }

    // 下班
    public void leave() {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP + "/User/UserLeave";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        responseLeave = gson.fromJson(response, ResponseLeave.class);
                        if (delegate != null)
                            delegate.onResponse(response, UserLeave.this);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                if (delegate != null)
                    delegate.onErrorResponse(error, UserLeave.this);
            }

        }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                Gson gson = new Gson();
                map.put("data", gson.toJson(leave));
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    public static interface Delegate {
        public void onResponse(String response, UserLeave userLeave);

        public void onErrorResponse(VolleyError error, UserLeave userLeave);
    }

    public static class Leave {
        private String attend_Date;
        private String user_ID;
        private String attend_LeaveTime;

        public String getAttend_Date() {
            return attend_Date;
        }

        public void setAttend_Date(String attend_Date) {
            this.attend_Date = attend_Date;
        }

        public String getUser_ID() {
            return user_ID;
        }

        public void setUser_ID(String user_ID) {
            this.user_ID = user_ID;
        }

        public String getAttend_LeaveTime() {
            return attend_LeaveTime;
        }

        public void setAttend_LeaveTime(String attend_LeaveTime) {
            this.attend_LeaveTime = attend_LeaveTime;
        }
    }

    public static class LeaveStatus {
        private static final String FAILED = "0";
        private static final String SUCCESS = "1";
        private static final String NOATTEND = "2";
    }

    public static class ResponseLeave {
        private String status;

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }
    }
}
